//! Let 语句

use crate::lexer::token::Token;
use crate::parser::expression::Expression;
use crate::parser::identifier::Identifier;
use crate::parser::node::{Node, NodeRoot};
use crate::parser::statement::Statement;
use std::ops::Deref;

#[derive(Debug, Clone)]
pub struct LetStatement {
    node: NodeRoot,
    token: Token,
    name: Identifier,
    value: Box<dyn Expression>,
}

impl LetStatement {
    pub fn new(token: Token, name: Identifier, value: Box<dyn Expression>) -> Self {
        let s = format!(
            "This is a statement, left is a identifier: {}, right is a expression who's value is: {}",
            name.cast_to_node().get_literal(),
            value.cast_to_node().get_literal()
        );
        let node = NodeRoot::new(s, "LetStatement".to_string());
        Self {
            token,
            name,
            value,
            node,
        }
    }

    pub fn value(&self) -> &dyn Expression {
        self.value.as_ref()
    }

    pub fn name(&self) -> &Identifier {
        &self.name
    }
}

impl Statement for LetStatement {
    fn cast_to_node(&self) -> &dyn Node {
        &self.node
    }
}

impl Deref for LetStatement {
    type Target = NodeRoot;

    fn deref(&self) -> &Self::Target {
        &self.node
    }
}
